package com.yidu.hbats.service.impl.AtsBase;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.dao.AtsBaseSportMapper;
import com.yidu.hbats.model.AtsBaseSport;
import com.yidu.hbats.model.AtsBaseSportItem;
import com.yidu.hbats.service.atsBase.AtsBaseSportItemService;
import com.yidu.hbats.service.atsBase.AtsBaseSportService;
import com.yidu.springbootdemo.permission.action.vo.TmoduleVo;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.yidu.springbootdemo.permission.service.TmoduleService;
import com.yidu.utils.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: spring-boot-demo
 * @description: ${description}
 * @author: clg
 * @create: 2019-11-12 09:57
 */

@Service
public class AtsBaseSportServiceImpl implements AtsBaseSportService {

    @Resource
    AtsBaseSportMapper atsBaseSportMapper;
    @Resource
    TmoduleService tmoduleService;
    @Resource
    AtsBaseSportItemService atsBaseSportItemService;

    @Override

    public List<AtsBaseSport> selectAll(AtsBaseSport atsBaseSport, Page page) {
        PageHelper.offsetPage(page.getStart(), page.getRows());
        List<AtsBaseSport> list=atsBaseSportMapper.selectAll(atsBaseSport);
        PageHelper.startPage(page.getStart(), page.getRows());
        return list;
    }


    @Override
    public int saveOrUpdate(AtsBaseSport atsBaseSport) {

        return atsBaseSportMapper.saveOrUpdate(atsBaseSport);
    }

    @Override
    public int insertSelective(AtsBaseSport atsBaseSport, HttpServletRequest request) {
        int one = atsBaseSportMapper.insertSelective(atsBaseSport);
        insertZhiBiao(atsBaseSport,request);
        TmoduleVo tTmoduleVo = new TmoduleVo();
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        tTmoduleVo.setModName(atsBaseSport.getSportName());
        tTmoduleVo.setModId(uuid);
        tTmoduleVo.setModState("Y");
        tTmoduleVo.setModSite("pages/atsDataBatch/AtsDataBatch.html?sportId="+atsBaseSport.getSportId());
        tTmoduleVo.settMModId("20");
        tTmoduleVo.setModComment(atsBaseSport.getSportId().toString());
        tmoduleService.add(tTmoduleVo);
        return one ;
    }

    @Override
    public int deleteByPrimaryKey(Integer sportId) {

        return atsBaseSportMapper.deleteByPrimaryKey(sportId);
    }

    @Override
    public int updateIsva(AtsBaseSport atsBaseSport) {
        //根据体育项目id 删除
        atsBaseSportItemService.deleteByPrimaryKey2(atsBaseSport.getSportId());
        deleteMod(atsBaseSport.getSportId().toString());
        return atsBaseSportMapper.updateIsva(atsBaseSport);
    }

    @Override
    public AtsBaseSport selectByPrimaryKey(Integer sportId) {
        return atsBaseSportMapper.selectByPrimaryKey(sportId);
    }

    @Override
    public int updateByPrimaryKeySelective(AtsBaseSport atsBaseSport,HttpServletRequest request) {
        int one = atsBaseSportMapper.updateByPrimaryKeySelective(atsBaseSport);
        //先删除 ats_Base_Sport_Item 内关联的 指标在添加新指标
        //根据体育项目id 删除
        atsBaseSportItemService.deleteByPrimaryKey2(atsBaseSport.getSportId());
        insertZhiBiao(atsBaseSport,request);
        return one;
    }

    @Override
    public int selectCount(AtsBaseSport atsBaseSport) {
        return atsBaseSportMapper.selectCount(atsBaseSport);
    }

    @Override
    public int deleteAll(Integer ids) {
        return atsBaseSportMapper.deleteAll(ids);
    }

    @Override
    public List<AtsBaseSport> findAll() {
        return atsBaseSportMapper.findAll();
    }

    @Override
    public AtsBaseSport selectAth(Integer athleteId) {
        return atsBaseSportMapper.selectAth(athleteId);
    }

    @Override
    public AtsBaseSport selectCoachSport(Integer coachId) {
        return atsBaseSportMapper.selectCoachSport(coachId);
    }

    @Override
    public AtsBaseSport selectExpertSport(Integer experId) {
        return atsBaseSportMapper.selectExpertSport(experId);
    }

    @Override
    public int deleteMod(String sportId) {
        return atsBaseSportMapper.deleteMod(sportId);
    }


    /**
     * 共用添加或删除 体育项目后 插指标
     * @param atsBaseSport
     * @param request
     */
    public void insertZhiBiao(AtsBaseSport atsBaseSport, HttpServletRequest request){
        Tuser user = (Tuser) request.getSession().getAttribute("user");
        //插项目的指标
        for (int i = 0; i < atsBaseSport.getAtsBaseItem().length; i++) {
            AtsBaseSportItem tr = new AtsBaseSportItem();
            tr.setSportId(atsBaseSport.getSportId());
            Date date = new Date();
            tr.setCreatTime(date);
            if(!StringUtils.isEmpty(user)){
                tr.setCreater(user.getUserName());
            }else{
                tr.setCreater("HJJ");
            }
            tr.setIsva(1);//可用
            tr.setItemId(atsBaseSport.getAtsBaseItem()[i]);
            atsBaseSportItemService.insertSelective(tr);
        }
    }
}
